home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Learn Microsoft Visual Basic 6.0 Now
/
Learn Microsoft Visual Basic 6.0 Now (Microsoft Press)(X03-58607)(1998).ISO
/
media
/
chap11
/
b11d005.cc2
< prev
next >
Wrap
Text File
|
1998-06-07
|
6KB
|
131 lines
0, Okay, now we'll have some fun with
3, Microsoft Word. This demonstration shows how
5, you can use Automation from Visual Basic
8, to tap the features of Word 97 with
10, program code. Rather than build a spelling
13, and grammar checker from scratch in my
15, program, I'll use Word's impressive
17, capabilities and save myself some development
19, time. I've already built the program in
22, the development environment, so let's
24, take a look and see what it does. First,
28, to enable the Automation, I'll select
32, the References command from the Project
34, menu, and check to see that Microsoft
38, Word 8.0 Object Library has been
40, selected. And you can see right now, that I've
43, already made this selection. This is a
45, library that I need to have installed on
47, my system. And the way to do that is to
48, install Microsoft Word either separately
51, or as part of Microsoft Office 97. Now,
55, I can use an earlier version of Word,
57, but if I do, I have to have slightly
59, different program code to make the Word
61, Object Library work. So, click OK to verify
64, that. And now, let's take a look at the
67, objects I'm using on my form. This is a
70, text box that I'm going to type some text
72, in. What I'll do is type sort of a
75, sample sentence and then I'll use the Word's
78, grammar and spelling checker to analyze
80, it. And I have a Check Grammar button,
82, that will call up Microsoft Word to
85, check the grammar, and I have a Done button
88, to quit the program. The Check Grammar
89, button is the one that actually will call
92, my program code and create the
93, Automation. So, I'll double-click that now to
95, open the Code window and we'll take a look
99, at my program code. First, I used the
102, Dim statement to declare an object
104, variable. And then I used the Set statement to
108, put the Word.Application into the
110, object variable. From this time on, the
113, variable X will actually stand for
115, Word.Application. Then the first thing I do is
119, start up Word and make it invisible by setting
122, the Visible property to False. And I add
125, a new document to Word, so I can store
127, my text that comes from Visual Basic.
131, And then, I assign the text that's in the
134, text box object in Visual Basic to the
137, currently selected text in Microsoft
139, Word. Then, I call the grammar checker in
142, Word with the CheckGrammar method. And at
145, this point in my program, the Word
147, grammar checker appears on the screen in
150, Visual Basic. And I have a chance to check
153, the grammar and spelling of my text. I
156, may reply to some grammar suggestions, or
159, I may check the spelling of some words
161, and make some corrections. And when I'm
164, finished with that and the dialog box
166, closes, I want to go ahead and copy that
169, corrected text that's in the
171, Selection.Text property back to Visual Basic to
176, the Text1.Text property. So that will
179, copy the correction back to my Visual Basic
181, program. I want to go ahead and close
184, Word and not save my changes. And then
186, the last thing I do is quit Word, and that
189, closes Microsoft Word from running.
192, Now, if I don't quit Word, it will continue
194, running and taking up memory. So I want
196, to make sure I quit it when I'm
197, finished with my Automation. And finally, I
200, will release that object variable which is
203, taking up some memory right now, setting
205, the variable to Nothing. And that's my
208, program code. So let's go ahead and run
210, this and see how it works. Let's click
213, the Start button. So, my personal grammar
218, checker appears in a dialog box. And in
221, the text box, I can type some sample
223, text. How about, "thiss is a tesst
230, message." Notice that I have two s's in this
234, and it starts with a lower-case letter,
236, and two s's in test, and it ends with a
239, period. So, I have a sentence here with
241, lack of capitalization and two spelling
243, errors. Okay, now, I'll go ahead and click
246, this button to start up Microsoft Word.
250, And it doesn't take too long if you
251, think about it. Word is loading up now, and
253, starting, and has to display the
256, grammar checker feature that we're calling
258, through Automation. And after a moment, the
262, grammar checker appears. Word 97 has an
265, integrated spelling and grammer
266, checker. The first thing it notices is the
269, misspelled word thiss, and it highlights it
271, with red type, and suggests that we
272, change it to the word this. And so we'll
275, click the Change button and accept that
277, change. Then, the spelling grammar checker
280, notices that tesst is misspelled, and
282, highlights it with red type, and suggests
284, test. So we'll take that change, too.
287, And then it comes up with the
289, capitalization problem. This is in green type, so
291, not as serious of an error. But we'd like
292, to make that change, too, because we
294, want our sentence to be complete with
296, punctuation and capitalization. So, we'll
299, click Change. And that completes our
302, operation. And then in a moment, there's a
304, sort of a flash of the screen as Word
305, closes. And then the correct text is copied
309, from Word back into Visual Basic. And we
312, have the corrected text, "this is a test
314, message." So, you can see how slick
317, Word really is. We leveraged a dictionary
320, with many, many items that will help us
322, to spell our words correctly. We
324, leveraged a grammar checker, which supplies some
326, sophisticated parsing of the lines in
330, our test message, and we did that all
332, with just a few lines of program code. And
334, when we're done, we'll click the Done
336, button.
340, END